﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Linq;


namespace ToolExcelPub
{
    public static partial class XTool
    {
        //文件端类型
        public enum EToolFileType
        {
            无效的 = 0,
            枚举 = 1,
            公共类 = 1 << 1,
            服务器 = 1 << 2,
            客户端 = 1 << 3,
            公共 = (1 << 2) | (1 << 3),
        }
        //文件信息
        public class XFileInfo
        {
            public EToolFileType ToolType = EToolFileType.无效的;
            public string FileName = "文件名";
            public string FileTime = "修改时间";
            public bool NeedRead = false;
            public bool HasType(EToolFileType type)
            {
                return (ToolType & type) > 0;
            }
        }

        //读取全部文件
        public static void ReadFileList()
        {
            ShowInfo("ReadFileList!");

            DictXFileInfo.Clear();

            GetFileList(PathExcel);
        }
        //读取文件
        public static void GetFileList(string path)
        {
            if (Directory.Exists(path))
            {
                foreach (var dirname in Directory.GetDirectories(path))
                {
                    EToolFileType ToolType = EToolFileType.公共;
                    if (dirname.Contains("备份") || dirname.Contains("backup") || dirname.Contains("Backup"))
                    {
                        continue;
                    }
                    if (dirname.Contains("服务器") || dirname.Contains("server") || dirname.Contains("Server"))
                    {
                        ToolType = EToolFileType.服务器;
                    }
                    else if (dirname.Contains("客户端") || dirname.Contains("client") || dirname.Contains("Client"))
                    {
                        ToolType = EToolFileType.客户端;
                    }
                    GetFileInfo(dirname, ToolType);
                }
                GetFileInfo(path, EToolFileType.公共);
            }
        }
        public static void GetFileInfo(string path, EToolFileType type)
        {
            if (Directory.Exists(path))
            {
                foreach (string filename in Directory.GetFileSystemEntries(path))
                {
                    if (File.Exists(filename))
                    {
                        var ToolType = type;
                        string filename_excel = Path.GetFileNameWithoutExtension(filename);
                        FileInfo fi = new FileInfo(filename);
                        if (filename_excel.Contains("公共枚举")
                            || filename_excel.Contains("Enum"))
                        {
                            ToolType = EToolFileType.枚举;
                        }
                        if (filename_excel.Contains("公共类")
                            || filename_excel.Contains("Pclass"))
                        {
                            ToolType = EToolFileType.公共类;
                        }
                        XFileInfo finfo = new XFileInfo()
                        {
                            FileName = filename,
                            FileTime = fi.LastWriteTime.ToString("u"),
                            ToolType = ToolType,
                            NeedRead = true,
                        };
                        DictXFileInfo[finfo.FileName] = finfo;
                    }
                }
            }
        }
    }
}
